<template>
  <div class="orderHandleBtn">
    <template v-if="setBtns(data).length !== 0">
      <!-- 列表展示 -->
      <template v-if="type === 'list'">
        <!-- 待确认、待挂单订单的确认、拒绝按钮不显示在操作菜单内 -->
        <template
          v-if="
            isSeller() &&
              (data.orderStatus === orderDict.pricingStatus.wait.value ||
                data.orderStatus === orderDict.confirmStatus.wait.value)
          "
        >
          <c-button
            size="small"
            :type="
              item.name === '确认'
                ? 'primary'
                : item.name === '拒绝'
                ? 'danger'
                : ''
            "
            v-for="item in setBtns(data)"
            :key="item.flag"
            @click="clickEvent(item.clickEvent, data)"
            >{{ item.name }}</c-button
          >
        </template>
        <a-dropdown v-else>
          <a-menu slot="overlay">
            <a-menu-item @click="checkDetail">
              查看详情
            </a-menu-item>
            <a-menu-item
              v-for="item in setBtns(data)"
              :key="item.flag"
              @click="clickEvent(item.clickEvent, data)"
            >
              {{ item.name }}
            </a-menu-item>
          </a-menu>
          <a-button size="small">操作 <a-icon type="down"/></a-button>
        </a-dropdown>
      </template>
      <!-- 详情展示 -->
      <template v-else>
        <a-space>
          <c-button
            v-for="item in setBtns(data)"
            :key="item.flag"
            ghost
            type="primary"
            @click="clickEvent(item.clickEvent, data)"
          >
            {{ item.name }}
          </c-button>
        </a-space>
      </template>
    </template>
    <c-modal
      v-model="partDealVisible"
      title="部分成交"
      :width="300"
      :okLoading="partDealAmountLoading"
      @ok="submitPartDeal"
      @cancel="cancelModal"
    >
      <p class="tl">
        输入值不得大于剩余吨数
        <span>
          {{
            $tools.formatFloatFloor(
              Number(data.amount) - Number(data.dealAmount)
            )
          }}
        </span>
      </p>
      <a-input
        v-model="partDealAmount"
        type="number"
        placeholder="请输入成交数"
      >
      </a-input>
    </c-modal>
    <c-modal
      title="订单备注"
      v-model="remarkVisible"
      :okLoading="remarkLoading"
      @ok="remarkSubmit"
      @cancel="cancelModal"
    >
      <a-textarea
        v-model="remark"
        placeholder="请输入订单备注内容"
      ></a-textarea>
    </c-modal>
  </div>
</template>

<script>
import orderDict from "@/dict/order";
import setBtns from "./btnJudge.js";
import {
  acceptconfirmorder,
  refuseconfirmorder,
  acceptslideorder,
  refuseslideorder,
  cancelconfirmorder,
  cancelslideorder,
  addcustomerorderremark,
  addproviderorderremark,
  providercancelslideorder,
  providercancelslidehedgeorder
} from "@/api/order";
import { isSeller } from "@/util/common";
import { close } from "./btnHandle/close.js";
import { tally } from "./btnHandle/tally.js";
import { revoke } from "./btnHandle/revoke.js";
import { modifyPrice } from "./btnHandle/modifyPrice.js";
import { modifyGoods } from "./btnHandle/modifyGoods.js";
import { deal } from "./btnHandle/deal.js";
import { contract } from "./btnHandle/contract.js";

export default {
  props: {
    data: {
      type: Object
    },
    // list：订单列表 detail：订单详情 用于展示不同按钮排版
    type: {
      type: String,
      default: "list"
    }
  },
  mixins: [revoke, tally, close, modifyPrice, modifyGoods, deal, contract],
  data() {
    return {
      orderDict,
      handleRow: undefined, // 操作的row
      remark: undefined, // 订单备注
      remarkVisible: false, // 订单备注弹窗
      remarkType: undefined, // 备注订单类型 0买家 1卖家
      remarkLoading: false // 备注订单提交loading
    };
  },
  methods: {
    isSeller,
    setBtns,
    clickEvent(event, row) {
      this[event](row, this);
    },
    // 查看详情
    checkDetail() {
      let name = this.$route.name + "Detail";
      this.$router.push({
        name: name,
        query: { id: this.data.id }
      });
    },
    // 关闭弹窗回调
    cancelModal() {
      this.handleRow = undefined;
      this.remark = undefined;
      this.partDealAmount = undefined;
      this.modifyPriceAmount = undefined;
      this.remarkType = undefined;
    },
    // 买家，取消确定价订单
    cancelConfirm(data, _this) {
      this.$confirm({
        title: "此操作将取消该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            cancelconfirmorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "cancelConfirm"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 买家，取消点价订单
    cancelPricing(data, _this) {
      this.$confirm({
        title: "此操作将取消该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            cancelslideorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "cancelPricing"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 卖家，确认确定价订单
    acceptConfirm(data, _this) {
      acceptconfirmorder({
        orderId: data.id,
        version: data.version
      }).then(res => {
        _this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: "acceptConfirm" });
      });
    },
    // 卖家，确认点价订单
    acceptPricing(data) {
      acceptslideorder({
        orderId: data.id,
        version: data.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: "acceptPricing" });
      });
    },
    // 卖家，拒绝确定价订单
    refuseConfirm(data, _this) {
      this.$confirm({
        title: "此操作将拒绝该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            refuseconfirmorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "refuseConfirm"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 卖家，拒绝点价订单
    refusePricing(data, _this) {
      this.$confirm({
        title: "此操作将拒绝该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            refuseslideorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "refusePricing"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 卖家，取消点价订单 已挂单未成交
    sellerCancelPricing(data, _this) {
      this.$confirm({
        title: "此操作将取消该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            providercancelslideorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "sellerCancelPricing"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 卖家，取消点价订单（套保） 已挂单未成交
    sellerCancelPricingHedge(data, _this) {
      this.$confirm({
        title: "此操作将取消该订单, 是否继续?",
        onOk() {
          return new Promise(resolve => {
            providercancelslidehedgeorder({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "sellerCancelPricingHedge"
                });
              })
              .finally(() => {
                resolve();
              });
          });
        }
      });
    },
    // 备注订单提交
    remarkSubmit() {
      this.remarkLoading = true;
      let request =
        this.remarkType === 0 ? addcustomerorderremark : addproviderorderremark;
      request({
        orderId: this.handleRow.id,
        version: this.handleRow.version,
        remark: this.remark
      })
        .then(res => {
          this.remarkVisible = false;
          this.$emit("handleSuccess", { data: res, type: "remark" });
        })
        .finally(() => {
          this.remarkLoading = false;
        });
    },
    // 买家备注订单
    buyerRemarkOrder(row) {
      this.handleRow = row;
      this.remarkVisible = true;
      this.remarkType = 0;
    },
    // 卖家备注订单
    sellerRemarkOrder(row) {
      this.handleRow = row;
      this.remarkVisible = true;
      this.remarkType = 1;
    }
  }
};
</script>
